<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>商品列表</span>
    </div>

    <el-table :data="dataList" style="width: 98%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品ID:">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="商品价格:">
              <span>{{ props.row.price }}</span>
            </el-form-item>
            <el-form-item label="点击率:">
              <span>{{ props.row.rating }}</span>
            </el-form-item>
            <el-form-item label="商品销量:">
              <span>{{ props.row.sellCount }}</span>
            </el-form-item>
            <el-form-item label="创建时间:">
              <span>{{ props.row.ctime }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="category" label="商品分类"></el-table-column>
      <el-table-column prop="price" label="价格" width="120"></el-table-column>
      <el-table-column prop="thumbnail" label="商品图片">
        <template slot-scope="scope">
          <img :src="scope.row.imgUrl" min-width="70" height="70" />
        </template>
      </el-table-column>
      <el-table-column prop="goodsDesc" label="商品描述"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="primary" @click="eidt(scope.row)">编辑</el-button>
          <el-button type="danger" @click="clidel(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="sizechange"
        @current-change="pagechange"
        :current-page="currentPage"
        :page-sizes="pagesizes"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog title="商品编辑" :visible.sync="dialogVisible" width="35%">
      <p class="df aic">
        <span class="mr10">商品名称</span>
        <el-input v-model="name"></el-input>
      </p>
      <p class="df aic">
        <span class="mr10">商品分类</span>
        <el-select v-model="type" style="margin: 0">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.cateName"
            :value="item.cateName"
          >
          </el-option>
        </el-select>
      </p>
      <p class="df aic">
        <span class="mr10">商品价格</span>
        <el-input-number v-model="price" :min="0.01"></el-input-number>
      </p>
      <div class="df aic avatarbox">
        <span class="mr10">商品图片</span>
        <el-upload
          class="avatar-uploader"
          action="http://127.0.0.1:5000/goods/goods_img_upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          style="width:100px,height:100px"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <!-- <span class="massge">请上传新图片</span> -->
      </div>
      <p class="df aic">
        <span class="mr10">商品描述</span>
        <el-input v-model="describe" type="textarea" style="flex: 1"></el-input>
      </p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="ok">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { itemlist, delitem, categories, edit } from "@/apis/item";
import { getYYYYMMDD } from "@/utils/utils";
export default {
  data() {
    return {
      currentPage: 1,
      pagesizes: [5, 10, 15, 20],
      pagesize: 5,
      dataList: [],
      total: 100,
      dialogVisible: false,
      name: "",
      type: "",
      price: "",
      describe: "",
      options: [],
      imageUrl: "",
    };
  },

  methods: {
    sizechange(newsize) {
      this.pagesize = newsize;
      this.getList();
    },
    pagechange(newpage) {
      this.currentPage = newpage;
      this.getList();
    },
    //获取列表
    getList() {
      itemlist({ currentPage: this.currentPage, pageSize: this.pagesize }).then(
        (res) => {
          console.log(res);
          for (const obj of res.data.data) {
            obj.ctime = getYYYYMMDD(obj.ctime);
          }
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      );
    },

    //删除商品
    clidel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delitem({ id: id }).then((res) => {
            if (res.data.code == 0) {
              this.$message({
                message: "系统提示:商品删除成功!",
                type: "success",
                center: true,
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
            center: true,
          });
        });
    },
    //图片上传
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      this.newimg = res.imgUrl;
      this.imageUrl = URL.createObjectURL(file.raw);
    },

    //修改商品
    eidt(obj) {
      this.dialogVisible = true;
      this.id = obj.id;
      this.name = obj.name;
      this.type = obj.category;
      console.log(obj.category);
      this.price = obj.price;
      this.imageUrl = obj.imgUrl;
      this.oldimg = obj.imgUrl;
      this.describe = obj.goodsDesc;
    },

    //保存数据
    ok() {
      this.dialogVisible = false;
      edit({
        name: this.name,
        category: this.type,
        price: this.price,
        imgUrl: this.newimg,
        goodsDesc: this.describe,
        id: this.id,
      }).then((res) => {
        if (res.data.code == 0) {
          this.$message({
            message: "恭喜你，商品修改成功",
            type: "success",
            center: true,
          });
          this.getList();
        }
      });
    },
  },
  created() {
    this.getList();
    categories().then((res) => {
      for (const obj of res.data.categories) {
        this.options.push(obj);
      }
    });
  },
};
</script>

<style lang="less" scoped>
.el-table {
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
}
.block {
  margin-top: 20px;
}
.el-dialog {
  p,
  .avatarbox {
    width: 100%;
    margin-bottom: 15px;
    span {
      width: 70px;
    }
    .el-input {
      flex: 1;
    }
  }
  .avatar-uploader .el-upload {
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
  .massge {
    color: #ff9999;
  }
}
</style>